package algorithm.middle;



/**
 * 给定两个单词 word1 和 word2，找到使得 word1 和 word2 相同所需的最小步数，每步可以删除任意一个字符串中的一个字符。
 */
public class MinDistance583 {
    /* 转化为最长公共子序列问题
     * https://leetcode-cn.com/problems/longest-common-subsequence/solution/zui-chang-gong-gong-zi-xu-lie-by-leetcod-y7u0/
     */
    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int[][] ints = new int[m + 1][n + 1];
        for (int i = 1; i < m + 1; i++) {
            char w1 = word1.charAt(i - 1);
            for (int j = 1; j < n + 1; j++) {
                char w2 = word2.charAt(j - 1);
                if (w1 == w2) {
                    ints[i][j] = ints[i - 1][j - 1] + 1;
                } else {
                    ints[i][j] = Math.max(ints[i - 1][j], ints[i][j - 1]);
                }
            }
        }
        return m + n - 2 * ints[m][n];
    }


}
